package demo8;

import java.util.Arrays;

public class Test {
    public static void main(String[] args) {
        Student[] students = new Student[] {
                new Student("xiaoming",18),
                new Student("zhangsan",16),
                new Student("wangwu",30)
        };
        System.out.println("排序前"+Arrays.toString(students));
        NameComparator nameComparator = new NameComparator();
        Arrays.sort(students,nameComparator);
        AgeComparator ageComparator = new AgeComparator();
        //Arrays.sort(students,ageComparator);
        System.out.println("排序后"+Arrays.toString(students));
    }


    public static void mysort(Comparable[] comparables) {
        for (int i = 0; i < comparables.length - 1; i++) {
            for (int j = 0; j < comparables.length - 1 - i; j++) {
                if(comparables[j].compareTo(comparables[j + 1]) > 0) {
                    Comparable comparable = comparables[j];
                    comparables[j] = comparables[j+1];
                    comparables[j+1] = comparable;
                }
            }
        }
    }
    public static void main2(String[] args) {
        Student[] students = new Student[] {
                new Student("zhansan",40),
                new Student("lisi",35),
                new Student("wangwu",50)
        };
        System.out.println("排序前"+Arrays.toString(students));

        //Arrays.sort(students);
        mysort(students);
        System.out.println("排序后"+Arrays.toString(students));

    }
    public static void main1(String[] args) {
        Student student1 = new Student("lisi", 20);
        Student student2 = new Student("zhangsna", 30);

        //System.out.println(student1 < student2);
        // 对象的比较，存在两个问题
        //1、是不知比较哪个成员，来比较
        //2、比较的方法如何去写
        //我们需要用到compareTo的方法，但是需要实现接口Comparable
        //System.out.println(student1.compareTo(student2));
    }
}
